<?php
class Machine extends BaseDB
{
	function __construct()
	{
		$this->table = 'machine';
		parent::__construct();
	}
	
	public function add($data)
	{
		if (empty($data['name']) || empty($data['cartridges_type']))return false;
		return $this->insert($data);
	}
	
	public function addAll($data)
	{
		if (is_array($data))return false;
		foreach($data as $item){
			$this->add($item);
		}
		return true;
	}
	
	public function getInfoById($id)
	{
		if (empty($id)) return false;
		$res = $this->select(array('id'=>$id));
		if (empty($res)) {
			return false;
		}
		return $res[0];
	}
	
	public function getInfoByName($name)
	{
		if (empty($name))return false;
		$res = $this->select(array('name'=>$name));
		if (empty($res)) {
			return false;
		}
		return $res[0];
	}
	
	public function getInfoByNames($names = array()) 
	{
		if (empty($names))return false;
		$sql = "select name,cartridges_type from " . $this->table . " where name in (" . $this->getSelectInRangeStr($names) . ")";
		$res = $this->db->getAll($sql, $names);
		$ret = array();
		foreach($res as $k=>$v) {
			$ret[$v['name']] = $v['cartridges_type'];
		}
		return array('total'=>count($ret), 'data'=>$ret);
	}
	
	public function getList($start = 0, $count = 200)
	{
		$data = $this->db->getAll("select * from " . $this->table . " order by name limit $start, $count", array());
		return array(
			'total' => $this->getCount($where), 
			'data' => $data
		);
	}
	
	public function modify($data, $where) {
		if (empty($data) || empty($where)) {
			return false;
		}
		
		return $this->update($data, $where);
	}
}
?>